function sendEvent(typ,prm) { 
  thisMovie(MediaPlayer.playerObjectName).sendEvent(typ,prm); 
};
function getUpdate(typ,pr1,pr2,swf) {
	if(typ == "time") { 
		MediaPlayer.currentPosition = pr1; 
		if (pr2) MediaPlayer.currentRemaining = Math.round(pr2);  
		MediaPlayer.currentLength = MediaPlayer.currentPosition + MediaPlayer.currentRemaining;
		MediaPlayer.eventUpdateTime();
	}
	else if(typ == "volume") { MediaPlayer.currentVolume = pr1; MediaPlayer.eventVolumeChange();} 
	else if(typ == "item") { MediaPlayer.currentItemIndex = pr1; setTimeout("MediaPlayer.updateItemInfo()",100);}
	else if(typ == "state") {MediaPlayer.currentState = pr1;  MediaPlayer.eventStateChange();}
	else if(typ == "load") { MediaPlayer.currentLoad = pr1; MediaPlayer.eventLoadChange();}
	else if(typ == "size") { MediaPlayer.currentXsize = "X=" + pr1; pr2 == undefined ? null: currentYsize = "Y=" + Math.round(pr2); } 
};
function thisMovie(swf) {
  if(navigator.appName.indexOf("Microsoft") != -1) {
    return window[swf];
  } else {
    return document[swf];
  }
};

MediaPlayer = {
	playerObjectName	: 	"objMediaPlayer",
	currentPosition		:   0,
	currentRemaining	: 	0,
	currentLength		: 	0,
	currentVolume		: 	0,
	currentItemIndex	: 	0,
	currentState		: 	0,
	currentLoad			: 	0,
	
	playHistory			: 	{},
	
	defaultScrub		: 	10,
	playerWidth			: 	'100%',
	playerHeight		: 	20,
	bgColor				: 	"#D6DAC0",
	fgColor				: 	"0x000000",
	rolloverColor		: 	"#CC0066",
	showTimer			: 	true,
	showControls		: 	false,
	
	eventUpdateTime		: 	function () {},
	eventStateChange	: 	function () {},
	eventSongChange		: 	function () {},
	eventLoadChange		: 	function () {}, 
	eventVolumeChange	: 	function () {},
	
	STATE_PAUSED		: 	0,
	STATE_LOADING		: 	1,
	STATE_PLAYING		: 	2,
	STATE_FINISHED		: 	3 
};

MediaPlayer.updateItemInfo = function () {
	MediaPlayer.currentLength = MediaPlayer.currentSong().duration;
	MediaPlayer.eventSongChange();
}

MediaPlayer.build = function (container) {
	var so = new SWFObject('libs/mediaplayer/mediaplayer.swf',this.playerObjectName,this.playerWidth,this.playerHeight,'8');
		so.addParam('allowscriptaccess','always');
	  	so.addParam('allowfullscreen','false');
	  	so.addVariable('width',this.playerWidth);
	  	so.addVariable('height',this.playerHeight);
	//	so.addVariable('file','images/itunesPlayIcon.gif');
		so.addVariable('backcolor', this.bgColor);
		so.addVariable('frontcolor', this.fgColor);
		so.addVariable('lightcolor', this.rolloverColor);
		so.addVariable('autostart','false');
		so.addVariable('shownavigation', this.showControls);
		so.addVariable('usefullscreen', 'false');
		so.addVariable('showdigits', this.showTimer)
	  	so.addVariable('javascriptid',this.playerObjectName);
	  	so.addVariable('enablejs','true');
		so.addVariable('repeat','list');
		so.addVariable('shuffle', 'false');
	  	so.write(container);
}

MediaPlayer.playing = function () {
	return (this.currentState == this.STATE_PLAYING);
}

MediaPlayer.nextTrack = function () {
	sendEvent("next");
}

MediaPlayer.previousTrack = function () {
	sendEvent("prev");
}

MediaPlayer.back = function () {
	if (this.currentPosition < 10) this.previousTrack();
	else this.restartSong();
}

MediaPlayer.stopAll = function () {
	sendEvent("stop");
}

MediaPlayer.togglePlayback = function () {
	sendEvent("playpause");
}

MediaPlayer.setVolume = function (percent) {
	sendEvent("volume", percent);
}

MediaPlayer.setPosition = function (playTime) {
	sendEvent('scrub',playTime);
}

MediaPlayer.restartSong = function () {
	this.setPosition(0);
}

MediaPlayer.scrubForward = function (distance) {
	if (distance) 	this.setPosition(MediaPlayer.currentPosition + distance);
	else 			this.setPosition(MediaPlayer.currentPosition + MediaPlayer.defaultScrub);
}

MediaPlayer.scrubBackward = function (distance) {
	if (distance) 	this.setPosition(MediaPlayer.currentPosition - distance);
	else 			this.setPosition(MediaPlayer.currentPosition - MediaPlayer.defaultScrub);
}

MediaPlayer.loadFile = function (filePath, fileName, fileAuthor, uniqueID) {
	var obj = {
		file	: 	filePath,
		title	: 	fileName,
		author	: 	fileAuthor,
		id		: 	uniqueID
	}
	this.playHistory[this.playHistory.length] = obj;
	thisMovie(MediaPlayer.playerObjectName).loadFile(obj);
	if (!this.playing()) this.togglePlayback();
}

MediaPlayer.appendToPlaylist = function (filePath, fileName, fileAuthor, uniqueID, fileAlbum) {
	var obj = {
		file	: 	filePath,
		title	: 	fileName,
		author	: 	fileAuthor,
		id		: 	uniqueID,
		album	: 	fileAlbum
	}
	thisMovie(MediaPlayer.playerObjectName).addItem(obj);
	
	if (this.defaultState()) this.removeTrack(0);
}

MediaPlayer.appendAndPlay = function (filePath, fileName, fileAuthor, uniqueID, fileAlbum) {
	MediaPlayer.appendToPlaylist(filePath, fileName, fileAuthor, uniqueID, fileAlbum);
	MediaPlayer.playTrack(MediaPlayer.playlistLength()-1);
}

MediaPlayer.playTrack = function (index) {
	sendEvent('playitem',index);
}

MediaPlayer.removeTrack = function (index) { 
	thisMovie(MediaPlayer.playerObjectName).removeItem(index);
}

MediaPlayer.removeAllTracks = function (index) { 
	while (MediaPlayer.playlistLength() > 1) {
		MediaPlayer.removeTrack(0);
	}
}

MediaPlayer.currentSong = function () {
	this.currentSongObj = this.playlistItem(this.currentItemIndex);
	return this.currentSongObj;
}

MediaPlayer.playlistItem =  function (index) {
	return thisMovie(MediaPlayer.playerObjectName).itemData(index);
}

MediaPlayer.defaultState = function () {
	var cs = this.playlistItem(0);
	return (cs.duration==0 && cs.file=='video.flv');
}

MediaPlayer.Playlist = function () {
	var x = this.playlistLength();
	var pList = {};
	for (var i=0;i<x;i++) {
		pList[i] = this.playlistItem(i);
	}
	return pList;
}

MediaPlayer.playlistLength = function () {
	return (thisMovie(MediaPlayer.playerObjectName).getLength());
}

LoadProgress.completeModule('MediaPlayer');
